// pages/zhzs/detail.js
import { getCardLog, addressQuery, addressTrajectory, getVolunteer } from '../../utils/my'
const objectHelper = require('../../utils/object-helper')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: '',
        title:{
            1: '用卡日志查询',
            2: '用卡场所查询',
            3: '用卡轨迹查询',
            4: '志愿者信息查询'
        },
        cardLogList:[],
        addressInfoList:[],
        volunteerInfo:{},
        addressTrajectoryList:[],
        pageNum: 0,
        pageSize: 20,
        hasMore: true,
        loading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const type = options.type || '';
        this.setData({
            type,
            pageNum: 0,
            hasMore: true,
            loading: false,
            addressInfoList: []
        });
        wx.setNavigationBarTitle({
            title: this.data.title[type] || '详情'
        })
        this.requestData()
    },
    requestData(options = {}) {
        const { isLoadMore = false, pageNum = isLoadMore ? this.data.pageNum : 0 } = options;
        let params = {
            // sfz: wx.getStorageSync('userInfo').idNumber,
            // name: wx.getStorageSync('userInfo').name
            sfz: '32010619921208324X',
            name: '李天然',

        }
        if (this.data.type == 1) {
            getCardLog(params).then(res => {
                this.setData({
                    cardLogList: res.data.cardLogList
                })
            }).catch(error => {
                console.error('getCardLog error', error);
                wx.showToast({
                    title: '数据加载失败',
                    icon: 'none'
                });
            }).finally(() => {
                wx.stopPullDownRefresh();
            })
        }
        if (this.data.type == 2) {
            if (this.data.loading) {
                return;
            }
            if (isLoadMore && !this.data.hasMore) {
                return;
            }
            const previousPage = this.data.pageNum;
            const targetPage = isLoadMore ? pageNum : 0;
            const pendingData = { loading: true };
            if (!isLoadMore) {
                pendingData.hasMore = true;
                pendingData.addressInfoList = [];
            }
            this.setData(pendingData);
            params.pagenum = targetPage;
            params.pagesize = this.data.pageSize;
            addressQuery(params).then(res => {
                const fetchedList = (res && res.data && res.data.addressInfoList) ? res.data.addressInfoList : [];
                const mergedList = isLoadMore ? this.data.addressInfoList.concat(fetchedList) : fetchedList;
                const hasMore = fetchedList.length === this.data.pageSize;
                this.setData({
                    addressInfoList: mergedList,
                    hasMore,
                    pageNum: targetPage
                })
                if (!hasMore && mergedList.length > 0) {
                    console.info('addressQuery: no more data to load');
                }
            }).catch(error => {
                console.error('addressQuery error', error);
                if (isLoadMore) {
                    this.setData({
                        pageNum: previousPage
                    });
                }
                wx.showToast({
                    title: '数据加载失败',
                    icon: 'none'
                });
            }).finally(() => {
                this.setData({
                    loading: false
                });
                wx.stopPullDownRefresh();
            })
            return;
        }
        if (this.data.type == 3) {
            addressTrajectory(params).then(res => {
                // 预处理数据，将对象转换为键值对数组
                const processedList = res.data.useLogCountList.map(item => {
                    return {
                        ...item,
                        list: item.list.map(item2 => {
                            // 使用工具函数处理对象
                            return objectHelper.formatObjectForDisplay(item2, {
                                showEmpty: false,
                                maxLength: 200,
                                dateFormat: true,
                                numberFormat: true
                            });
                        })
                    };
                });
                
                this.setData({
                    addressTrajectoryList: processedList
                })
            }).catch(error => {
                console.error('addressTrajectory error', error);
                wx.showToast({
                    title: '数据加载失败',
                    icon: 'none'
                });
            }).finally(() => {
                wx.stopPullDownRefresh();
            })
        }
        if (this.data.type == 4) {
            getVolunteer(params).then(res => {
                this.setData({
                    volunteerInfo: res.data
                })
            }).catch(error => {
                console.error('getVolunteer error', error);
                wx.showToast({
                    title: '数据加载失败',
                    icon: 'none'
                });
            }).finally(() => {
                wx.stopPullDownRefresh();
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        if (this.data.type == 2) {
            this.setData({
                pageNum: 0,
                hasMore: true
            });
        }
        this.requestData()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.type == 2) {
            if (this.data.loading || !this.data.hasMore) {
                return;
            }
            const nextPage = this.data.pageNum + 1;
            this.requestData({
                isLoadMore: true,
                pageNum: nextPage
            });
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})